﻿using System;
using System.Collections.Generic;
using System.Linq;
using Controllers;
using System.Configuration;

namespace Web
{
    public partial class EmpExamEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                string id = Request.QueryString["id"];
                if (!string.IsNullOrEmpty(id) && id != "undefined")
                {
                    var v = ctx.EmpExams.Single(r => r.Id == int.Parse(id));
                    StartDate.Value = v.StartDate.ToString("yyyy-MM-dd");
                    
                    EndDate.Value = ((DateTime)v.EndDate).ToString("yyyy-MM-dd");

                    txtEvltion.Value = v.ExamEvaluation;
                    txtOpinion.Value = v.PersonalOpinion;
                    txtPurpose.Value = v.ExamPurpose;
                    txtRemark.Value = v.Remark;
                    txtResult.Value = v.ExamResult;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            string id = Request.QueryString["id"];
            EmpExam emp;
            if (!string.IsNullOrEmpty(id) && id != "undefined")
            {
                emp = ctx.EmpExams.Single(r => r.Id == int.Parse(id));
            }
            else
            {
                emp = new EmpExam();
                emp.EmpId = int.Parse(Request.QueryString["empId"]);
                ctx.EmpExams.InsertOnSubmit(emp);
            }
            emp.StartDate = DateTime.Parse(StartDate.Value);

            emp.EndDate = DateTime.Parse(EndDate.Value);

            emp.ExamEvaluation = txtEvltion.Value.Trim();
            emp.ExamPurpose = txtPurpose.Value.Trim();
            emp.PersonalOpinion = txtOpinion.Value.Trim();
            emp.Remark = txtRemark.Value.Trim();
            emp.ExamResult = txtResult.Value.Trim();

            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='" + emp.Id + "';window.close();</script>");
        }
    }
}